package main

import (
	"day7pro/goweb/base7/gee"
	"log"
	"net/http"
	"time"
)

// step7 中间件

func onlyForV2() gee.HandleFunc {
	return func(c *gee.Context) {
		// start timer
		t := time.Now()
		c.Fail(500, "Internal Server Error\n")
		log.Printf("[%d] %s in %v", c.StatusCode, c.Req.RequestURI, time.Since(t))
	}
}

func main() {
	r := gee.New()
	r.Use(gee.Logger()) // global middleware
	r.GET("/", func(c *gee.Context) {
		c.HTML(http.StatusOK, "<h1>Index Page</h1>")
	})

	v2 := r.Group("/v2")
	v2.Use(onlyForV2()) // v2 group middleware
	v2.GET("/", func(c *gee.Context) {
		// expect /hello?name=xxx
		c.String(http.StatusOK, "<h1>Hello Gee v2</h1>")
	})
	v2.GET("/hello", func(c *gee.Context) {
		// expect /hello?name=xxx
		c.String(http.StatusOK, "hello2 %s, you are at %s\n", c.Query("name"), c.Path)
	})
	r.Run(":8000")
}
